from pprint import pprint
from wsgiref import headers

from RequestTool.requestTool import RequestTool


class SmsCouponController:
    __url = "http://localhost:8080"

    @classmethod
    def getRequest(cls, this_path, params=None, data=None, headers=None, json=None):
        """
        get请求的封装
        :param this_path: 当前请求的资源路径
        :param params: 当前请求的查询数据
        :param data: 当前请求的请求体json类型
        :param headers: 当前请求的请求头
        :param json: 当前请求的请求体json类型
        :return: RequestTool工具类的返回响应体和基本信息
        """
        this_url = cls.__url + this_path
        return RequestTool.request_def(this_url, "get", params=params, headers=headers, json=json, data=data)

    @classmethod
    def postRequest(cls, this_path, params=None, data=None, headers=None, json=None):
        this_url = cls.__url + this_path
        return RequestTool.request_def(this_url, "post", params=params, headers=headers, json=json, data=data)


if __name__ == '__main__':
    #  1.登录拿到token
    #  获取到通用授权请求头
    header = RequestTool.getToken()

    # 2.添加优惠券 /coupon/create
    coupon_create_json = {
        "amount": 0,
        "code": "string",
        "count": 0,
        "enableTime": "2024-06-10T14:29:28.548Z",
        "endTime": "2024-06-10T14:29:28.548Z",
        "id": 0,
        "memberLevel": 0,
        "minPoint": 0,
        "name": "测试优惠券1",
        "note": "string",
        "perLimit": 0,
        "platform": 0,
        "productCategoryRelationList": [
            {
                "couponId": 0,
                "id": 0,
                "parentCategoryName": "string",
                "productCategoryId": 0,
                "productCategoryName": "string"
            }
        ],
        "productRelationList": [
            {
                "couponId": 0,
                "id": 0,
                "productId": 0,
                "productName": "string",
                "productSn": "string"
            }
        ],
        "publishCount": 0,
        "receiveCount": 0,
        "startTime": "2024-06-10T14:29:28.548Z",
        "type": 0,
        "useCount": 0,
        "useType": 0
    }
    pprint(SmsCouponController.postRequest("/coupon/create", headers=header, json=coupon_create_json))

    #  3.根据name查询优惠券 /coupon/list
    coupon_list_params = {
        "name": "测试优惠券1"
    }
    response_query = SmsCouponController.getRequest("/coupon/list",params=coupon_list_params, headers=header)
    coupon_list = response_query.get("data").get("data").get("list")
    lenCoupon = len(coupon_list)
    #  拿到最新的额添加的数据id
    new_id = coupon_list[lenCoupon-1].get("id")

    #  4.根据id修改优惠券 /coupon/update/{id}
    update_json = {
        "amount": 0,
        "code": "string",
        "count": 0,
        "enableTime": "2024-06-11T00:55:40.942Z",
        "endTime": "2024-06-11T00:55:40.942Z",
        "id": 0,
        "memberLevel": 0,
        "minPoint": 0,
        "name": "小米手机专用券",
        "note": "string",
        "perLimit": 0,
        "platform": 0,
        "productCategoryRelationList": [
            {
                "couponId": 0,
                "id": 0,
                "parentCategoryName": "string",
                "productCategoryId": 0,
                "productCategoryName": "string"
            }
        ],
        "productRelationList": [
            {
                "couponId": 0,
                "id": 0,
                "productId": 0,
                "productName": "string",
                "productSn": "string"
            }
        ],
        "publishCount": 0,
        "receiveCount": 0,
        "startTime": "2024-06-11T00:55:40.942Z",
        "type": 0,
        "useCount": 0,
        "useType": 0
    }
    pprint(SmsCouponController.postRequest(f"/coupon/update/{new_id}", headers=header, json=coupon_create_json))

    #  5.根据id查看优惠券详情 /coupon/{id}
    pprint(SmsCouponController.getRequest("/coupon/list", params=coupon_list_params, headers=header))

    #  5. 删除优惠券 /coupon/delete/{id}
    pprint(SmsCouponController.postRequest(f"/coupon/delete/{new_id}", headers=header, json=coupon_create_json))
